package com.googlecode.jmicroerp.wicket.administrare;

import com.googlecode.jmicroerp.model.NCategorie;
import com.googlecode.jmicroerp.service.GenericService;
import com.googlecode.jmicroerp.wicket.DefaultEditPage;
import org.apache.wicket.extensions.markup.html.form.DateTextField;
import org.apache.wicket.extensions.yui.calendar.DatePicker;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 *
 * @author alex
 */
public class EditareCategoriiPage extends DefaultEditPage {
    private NCategorie categorie;
    @SpringBean(name = "genericService")
    private GenericService<NCategorie> genericService;
    
    public EditareCategoriiPage() {
        categorie = new NCategorie();
        init();
    }
    
    public EditareCategoriiPage(Long id) {
        categorie = genericService.getById(NCategorie.class, id);
        init();
    }
    
    private void init() {
        Form<NCategorie> form = new Form<NCategorie>("form");
        form.setModel(new CompoundPropertyModel<NCategorie>(categorie));
        
        TextField<String> tfCod = new TextField<String>("cod");
        tfCod.setRequired(true);
        form.add(tfCod);
        
        TextField<String> tfDenumire = new TextField<String>("denumire");
        tfDenumire.setRequired(true);
        form.add(tfDenumire);
        
        TextField<String> tfCodCuloare = new TextField<String>("codCuloare");
        tfCodCuloare.setRequired(true);
        form.add(tfCodCuloare);
        
        DateTextField dtfDataInceput = new DateTextField("dataInceput");
        dtfDataInceput.add(new DatePicker());
        form.add(dtfDataInceput);
        
        DateTextField dtfDataSfarsit = new DateTextField("dataSfarsit");
        dtfDataSfarsit.add(new DatePicker());
        form.add(dtfDataSfarsit);
        
        form.add(new SubmitLink("submit") {

            @Override
            public void onSubmit() {
                if (categorie.getId() == null) {
                    genericService.insert(categorie);
                } else {
                    genericService.update(categorie);
                }
                setResponsePage(VizualizareCategoriiPage.class);
            }
            
        });
        
        form.add(new Link("renuntare") {

            @Override
            public void onClick() {
                setResponsePage(VizualizareCategoriiPage.class);
            }
            
        });
        
        add(form);
    }
}

